package com.keyancompany.zhongbaopingtai.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.fragment.DocsFragment;
import com.keyancompany.zhongbaopingtai.model.SharePager;

import java.util.List;

/**
 *  分享列表
 */
public class DocumentsAdapter extends RecyclerView.Adapter<DocumentsAdapter.ViewHolder> {

    private final List<SharePager> mValues;
    private final DocsFragment.OnListFragmentInteractionListener mListener;

    public DocumentsAdapter(List<SharePager> items, DocsFragment.OnListFragmentInteractionListener listener) {
        mValues = items;
        mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_share, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        SharePager pager = mValues.get(position);
        holder.mItem = pager;
        holder.shareName.setText("来自："+ pager.getUserName());
        holder.shareTime.setText(pager.getCreateTime());
        holder.fileName.setText(pager.getCatalogName());
        holder.fileType.setText("文件类型：" + (pager.getFiletype().equals("0") ? "文件":"文件夹"));

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    // Notify the active callbacks interface (the activity, if the
                    // fragment is attached to one) that an item has been selected.
                    mListener.onListFragmentInteraction(holder.mItem);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView shareName;
        public final TextView shareTime;
        public final TextView fileName;
        public final TextView fileType;
        public SharePager mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            shareName = (TextView) view.findViewById(R.id.share_username);
            shareTime = (TextView) view.findViewById(R.id.share_time);
            fileName = (TextView) view.findViewById(R.id.file_name);
            fileType = (TextView) view.findViewById(R.id.file_type);

        }

        @Override
        public String toString() {
            return super.toString() + " '" + fileName.getText() + "'";
        }
    }
}
